package main

import (
	"fmt"
	"path/filepath"

	"pool/pool_backend/config"
	"pool/pool_backend/internal/api"
	"pool/pool_backend/internal/repository"
	"pool/pool_backend/internal/service"
	"pool/pool_backend/pkg/database"
	"pool/pool_backend/pkg/logger"
)

func main() {
	// 加载配置
	configPath := filepath.Join("config", "config.yaml")
	err := config.LoadConfig(configPath)
	if err != nil {
		panic(fmt.Sprintf("Failed to load config: %v", err))
	}

	// 初始化日志
	err = logger.InitLogger(&config.AppConfig.Logger)
	if err != nil {
		panic(fmt.Sprintf("Failed to init logger: %v", err))
	}
	defer logger.Sync()

	// 初始化数据库
	err = database.InitDB(&config.AppConfig.Database)
	if err != nil {
		logger.Fatal("Failed to init database: " + err.Error())
	}

	// 创建数据库连接
	db := database.GetDB()

	// 创建仓库
	userRepo := repository.NewUserRepository(db)
	claimRepo := repository.NewClaimRepository(db)

	// 创建服务
	userService := service.NewUserService(userRepo, claimRepo)
	claimService := service.NewClaimService(userRepo, claimRepo)

	// 创建路由
	router := api.NewRouter(&config.AppConfig.Server, userService, claimService)
	router.Init()

	// 启动服务器
	port := fmt.Sprintf(":%d", config.AppConfig.Server.Port)
	logger.InfoF("Server starting on port %s", port)
	err = router.Run(port)
	if err != nil {
		logger.Fatal("Failed to start server: " + err.Error())
	}
} 